package com.yc.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.biz.OrderBiz;
import com.yc.dao.DBHelper;

public class OrderBizImpl implements OrderBiz {

	DBHelper db=new DBHelper();
	
	//查看类别 添加到购物车
	@Override
	public int addCart(int mno, int gno, int num) {
		//开始写sql
		String xx="select * from cartinfo where mno="+mno;
		List<Map<String, String>> list=db.findAll(xx, null);
		
		//有值
		String sql;
		if(list.size()>0){
			//有值，修改数量  总数量不变
			sql="update cartinfo set num=num+"+num+" where mno="+mno+" and gno="+gno;
		}else{
			//没有数据，就添加
			sql="insert into cartinfo values(null,"+mno+","+gno+","+num+")";
		}
		int result=db.doUpdate(sql, null);
		
		return result;
	}
	
	//购物车页面显示
	@Override
	public List<Map<String, String>> findGoods(int mno) {
		String sql = "select num,cartinfo.gno,gname,price,pics from cartinfo,goodsinfo where cartinfo.gno=goodsinfo.gno and mno="+mno;
		List<Map<String, String>> list = db.findAll(sql, null);
		return db.findAll(sql, null);
	}

	//删除商品
	@Override
	public int delGoods(int gno, int mno) {
		String sql="delete from cartinfo where gno="+gno+" and mno="+mno;
		int result=db.doUpdate(sql, null);
		return result;
	}

	//改变数量
	@Override
	public int changeGoodsNum(int mno, int gno, int num) {
		String sql="update cartinfo set num ="+num+" where mno="+mno+" and gno="+gno;
		int result=db.doUpdate(sql, null);
		return result;
	}

	//得到地址
	@Override
	public List<Map<String, String>> getAllAddr(int mno) {
		String sql="select * from addrinfo where mno="+mno;
		List<Map<String, String>> list=db.findAll(sql, null);
		return db.findAll(sql, null);
	}

	//得到购物车里的商品
	@Override
	public List<Map<String, String>> cartGoods(int mno) {
		String sql = "select num,cartinfo.gno,gname,price,pics from cartinfo,goodsinfo where cartinfo.gno=goodsinfo.gno and mno="+mno;
		List<Map<String, String>> list = db.findAll(sql, null);
		return db.findAll(sql, null);
	}
	
	//得到用户信息
	@Override
	public List<Map<String, String>> getInfo(int mno) {
		String sql="select * from addrinfo where mno="+mno;
		List<Map<String, String>> list=db.findAll(sql, null);
		return db.findAll(sql, null);
	}

	//添加地址
	@Override
	public int addAddress(int mno, String uname, String prov, String city, String area, String addr, String tel) {
		String sql="insert into addrinfo values(null,?,?,?,?,?,?,?,0)";
		List<Object> params=new ArrayList<Object>();
		params.add(mno);
		params.add(uname);
		params.add(tel);
		params.add(prov);
		params.add(city);
		params.add(area);
		params.add(addr);
		
		int result=db.doUpdate(sql, params);
		
		return result;
	}

	//设置默认地址
	@Override
	public int setDefault(int mno, int ano) {
		//先将所有地址设置为0  再改为1
		String sql2="update addrinfo set status=0 where mno="+mno;
		db.doUpdate(sql2, null);
		
		String sql="update addrinfo set status=1 where ano="+ano+" and mno="+mno;
		int result=db.doUpdate(sql, null);
		
		return result;
	}

}
